package com.itheima.controller;

import com.itheima.common.Code;
import com.itheima.common.Result;
import com.itheima.dto.AddressBookDto;
import com.itheima.service.AddressBookService;
import com.itheima.vo.AddressBookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author :WuJunJie
 * packageName :com.itheima.controller
 * fileName :AddressBookController
 * creationTime :2022-11-28 12:32
 */
@RestController
@RequestMapping("addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    @PostMapping
    public Result insertAddressBook(@RequestBody AddressBookDto addressBookDto){
        AddressBookVo insert=addressBookService.insertAddressBook(addressBookDto);
        if(insert!=null){
            return Result.success(insert);
        }
        return Result.error(Code.Forbidden.getCode());
    }


    @DeleteMapping
    public Result deleteAddressBooks(@RequestParam("ids") List<String> ids){
        Boolean update=addressBookService.deleteAddressBooks(ids);
        if (update){
            return Result.success();
        }
        return Result.error(Code.Forbidden.getCode());
    }

    @PutMapping
    public Result updateAddressBooks(@RequestBody AddressBookDto addressBookDto){
        Boolean update=addressBookService.updateAddressBooks(addressBookDto);
        if (update){
            return Result.success();
        }
        return Result.error(Code.Forbidden.getCode());
    }
    @PutMapping("default")
    public Result setDefaultAddressBooks(@RequestBody AddressBookDto addressBookDto){
        AddressBookVo setDefault=addressBookService.setDefaultAddressBooks(addressBookDto);
        return Result.success(setDefault);
    }

    @GetMapping("default")
    public Result selectDefaultAddressBook(){
        AddressBookVo addressBookVo=addressBookService.selectDefaultAddressBook();
        if (addressBookVo==null)
        {
            return Result.error(Code.NotFound.getCode());
        }
        return Result.success(addressBookVo);
    }

    @GetMapping("list")
    public Result selectAddressBooks(){
        List<AddressBookVo> select=addressBookService.selectAddressBooks();
        return Result.success(select);
    }

    @GetMapping("{id}")
    public Result selectAddressBookById(@PathVariable String id){
        AddressBookVo select=addressBookService.selectAddressBookById(id);
        return Result.success(select);
    }
}
